package com.hotfire.template.sms;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * http://www.webchinese.com.cn   此公司的SMS短信平台
 * 
 * 返回值说明：
 * http://www.webchinese.com.cn/api.shtml
 * 短信发送后返回值	说　明
		-1	没有该用户账户
		-2	接口密钥不正确 [查看密钥]
		不是账户登陆密码
		-21	MD5接口密钥加密不正确
		-3	短信数量不足
		-11	该用户被禁用
		-14	短信内容出现非法字符
		-4	手机号格式不正确
		-41	手机号码为空
		-42	短信内容为空
		-51	短信签名格式不正确
		接口签名格式为：【签名内容】
		-6	IP限制
		大于0	短信发送数量
 * @author jqsl2012@163.com
 *
 */
public class SMSWebChinese {
	public static void main(String[] args) throws HttpException, IOException {
		Sms sms = new Sms();
		sms.setPhone("18221303152");
		sms.setContent("hello【热火网络】");
		sendSMS(sms);
	}

	public static void sendSMS(Sms sms) throws IOException, HttpException,
			UnsupportedEncodingException {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn");
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码
		NameValuePair[] data = { new NameValuePair("Uid", "jqsl2012"),
				new NameValuePair("Key", "8376220944aae870d31a"),
				new NameValuePair("smsMob", sms.getPhone()),
				new NameValuePair("smsText", sms.getContent()) };
		post.setRequestBody(data);

		client.executeMethod(post);
		Header[] headers = post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		System.out.println("statusCode:" + statusCode);
		for (Header h : headers) {
			System.out.println("h.toString()="+h.toString());
		}
		String returnCode = new String(post.getResponseBodyAsString().getBytes(
				"gbk"));
		System.out.println("result="+returnCode);

		post.releaseConnection();
		
		sms.setReturnCode(returnCode);
	}
}
